<?php

class SiteController extends Controller
{
    public function actionIndex()
    {
        $new_customer = $this->connection->createCommand("select * from crm_customer limit 0,10")->queryAll();
        $new_contacter = $this->connection->createCommand("select * from crm_contacter limit 0,10")->queryAll();
        $new_contact_log = $this->connection->createCommand("select * from crm_contactlog limit 0,10")->queryAll();
        $new_memdays = $this->connection->createCommand("select * from crm_memdays limit 0,10")->queryAll();

        $this->render('index', ['new_customer' => $new_customer, 'new_contacter' => $new_contacter,
            'new_contact_log' => $new_contact_log, 'new_memdays' => $new_memdays]);
    }

    public function actionLogin(){
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];

        $user = $this->connection->createCommand("select * from crm_user where username = :username")
            ->bindParam(':username',$username,PDO::PARAM_STR)
            ->queryRow();

        if(is_null($user)){
            //用户不存在
            echo json_encode(Appcode::error_username);
        }else{
            //密码进行校验
            if($user['password'] == $password){
                Yii::app()->session['user']=$user;
                echo json_encode(Appcode::success);
            }else{
                echo json_encode(Appcode::error_password);
            }
        }

    }

}